<?php
if($this->do=='article'){
	$this->check_access('content_article');
	$content->check_category();
	$sql="SELECT * FROM ".DB_PREFIX."content_article WHERE 1=1";
	if(!empty($_GET['category_id'])){
		$sql.=" AND category_id='".$_GET['category_id']."'";
	}
	if(!empty($_GET['keyword'])){
		$sql.=" AND article_title like '%".trim($_GET['keyword'])."%'";
	}
	$sql.=" ORDER BY article_time DESC,article_id DESC";
	$page_size=$this->config['content_admin_article_list_size']?$this->config['content_admin_article_list_size']:20;
	$page_current=isset($_GET['page'])&&is_numeric($_GET['page'])?intval($_GET['page']):1;
	$count=$this->db->count($sql);
	$res=$this->db->result($sql." limit ".(($page_current-1)*$page_size).",".$page_size);
	$array=array();
	if($count>0){
		foreach($res as $row){
			$array[$row['article_id']]['id']=$row['article_id'];
			$array[$row['article_id']]['title']=$row['article_title'];
			$array[$row['article_id']]['author']=$row['article_author'];
			$array[$row['article_id']]['image']=$row['article_image'];
			$array[$row['article_id']]['html']=$row['article_html'];
			$array[$row['article_id']]['is_html']=$row['article_is_html'];
			$array[$row['article_id']]['url']=PATH.HTML_DIR.$content->category_parent_path($row['category_id'])."/".$row['article_html'].".html";
			$array[$row['article_id']]['time']=date("Y-m-d",$row['article_time']);
			$array[$row['article_id']]['comment_count']=$row['article_comment_count'];
			$array[$row['article_id']]['category_id']=$row['category_id'];
			$array[$row['article_id']]['category_name']=$this->db->value(DB_PREFIX."content_category","category_name","category_id=".$row['category_id']);
			$array[$row['article_id']]['is_best']=$row['article_is_best'];
			$array[$row['article_id']]['is_comment']=$row['article_is_comment'];
		}
		$parameter='action=content&do=article&';
		if(!empty($_GET['category_id'])){
			$sql.=" AND category_id='".$_GET['category_id']."'";
		}
		if(!empty($_GET['keyword'])){
			$parameter.="keyword='".trim($_GET['keyword'])."'";
		}
		$pagebar=pager(get_self(),$parameter,$page_current,$page_size,$count);
	}else{
		$pagebar="";
	}

	$this->template->in('article',$array);
	$this->template->in('pagebar',$pagebar);
	$this->template->out('content.article.list.php');
}
if($this->do=='article_add'){#内容添加 
	$this->check_access('content_article');
	$category_id=empty($_GET['category_id'])?0:intval($_GET['category_id']);
	$array=array();
	$array['category_id']=$category_id;
	$array['id']=0;
	$array['title']='';
	$array['content']='';
	$array['content_mode']=0;
	$array['author']='';
	$array['keywords']='';
	$array['description']='';
	$array['image']='';
	$array['html']=intval($this->db->value(DB_PREFIX."content_article","max(article_id)","1=1 ORDER BY article_id DESC"))+1;
	$array['is_best']=0;
	$array['is_comment']=1;
	$array['is_display']=1;
	$draft_id=isset($_GET['draft_id'])?intval($_GET['draft_id']):'';
	if($draft_id!=''){
		$row=$this->db->row("SELECT * FROM ".DB_PREFIX."content_draft WHERE draft_id=$draft_id");
		$array['title']=$row['draft_title'];
		$array['content']=$row['draft_content'];
		$array['author']=$row['draft_author'];
		$this->template->in('draft_id',$draft_id);
	}
	$this->template->in('article',$array);
	if(!empty($_GET['category_id'])){
		$this->template->in('category_option',$content->category_option(0,$_GET['category_id']));
	}else{
		if(!empty($_SESSION['category_id'])){
			$this->template->in('category_option',$content->category_option(0,$_SESSION['category_id']));
		}else{
			$this->template->in('category_option',$content->category_option());
		}
	}
	$this->template->in('mode','insert');
	$this->template->out('content.article.info.php');
}
if($this->do=='article_insert'){#内容插入
	$this->check_access('content_article');
	$this->set_hook('begin.article.insert');
	$article_id=empty($_POST['article_id'])?0:intval($_POST['article_id']);
	$article_title=empty($_POST['article_title'])?'':trim(addslashes($_POST['article_title']));
	$article_content=empty($_POST['article_content'])?'':trim(addslashes($_POST['article_content']));
	$article_content_mode=empty($_POST['article_content_mode'])?0:1;
	$article_author=empty($_POST['article_author'])?'':trim(addslashes($_POST['article_author']));
	$article_keywords=empty($_POST['article_keywords'])?'':trim(addslashes($_POST['article_keywords']));
	$article_description=empty($_POST['article_description'])?'':trim(addslashes($_POST['article_description']));
	$article_file=empty($_POST['article_file'])?'':trim(addslashes($_POST['article_file']));
	$article_html=empty($_POST['article_html'])?'':trim(addslashes($_POST['article_html']));
	$article_image=empty($_POST['article_image'])?'':trim(addslashes($_POST['article_image']));
	$article_is_best=empty($_POST['article_is_best'])?0:1;
	$article_is_comment=empty($_POST['article_is_comment'])?0:1;
	$article_is_display=empty($_POST['article_is_display'])?0:1;
	$category_id=empty($_POST['category_id'])?0:intval($_POST['category_id']);
	if($article_title==''){
		alert('标题不能为空');
	}
	if($article_content==''){
		alert('内容不能为空');
	}

	#本地方式上传
	$article_image_upload=upload($_FILES['article_image_upload'],HTML_DIR.'uploads/','jpg,png,gif,bmp',2);
	if(!empty($article_image_upload)){
		$article_image=PATH.HTML_DIR.'uploads/'.$article_image_upload;
	}
	$array=array();
	$array['article_title']=$article_title;
	$array['article_content']=$article_content;
	$array['article_content_mode']=$article_content_mode;
	$array['article_author']=$article_author;
	$array['article_keywords']=$article_keywords;
	$array['article_description']=$article_description;
	$array['article_file']=$article_file;
	$array['article_html']=$article_html;
	$array['article_image']=$article_image;
	$array['article_is_best']=$article_is_best;
	$array['article_is_comment']=$article_is_comment;
	$array['article_is_display']=$article_is_display;
	$array['article_time']=$_SERVER['REQUEST_TIME'];
	$array['category_id']=$category_id;
	$this->db->insert(DB_PREFIX."content_article",$array);
	$article_id=$this->db->id();
	#把关键字加入标签库
	if(!empty($_POST['tag'])&&!empty($article_keywords)){
		$tag_explode=explode(",",$article_keywords);
		if(count($tag_explode)>0){
			foreach($tag_explode as $v){
				if(!empty($v)){
					$tag_link=PATH.HTML_DIR.$content->category_parent_path($category_id)."/".$article_html.".html";
					$this->db->insert(DB_PREFIX."tag",array('tag_name'=>$v,'tag_link'=>addslashes($tag_link)));
				}
			}
		}
	}
	$_SESSION['category_id']=$category_id;
	
	$content->html_index();
	$content->html_article($article_id);
	$content->html_category_index($category_id);
	$content->html_category_list($category_id);
	$this->set_hook('end.article.insert');
	redirect('?action=content&do=article');
}
//编辑内容
if($this->do=='article_edit'){
	$this->check_access('content_article');
	$article_id=empty($_GET['article_id'])?0:intval($_GET['article_id']);
	$row=$this->db->row("SELECT * FROM ".DB_PREFIX."content_article WHERE article_id='".$article_id."'");
	$array=array();
	$array['id']=$row['article_id'];
	$array['title']=$row['article_title'];
	$array['content']=$row['article_content'];
	$array['content_mode']=$row['article_content_mode'];
	$array['author']=$row['article_author'];
	$array['keywords']=$row['article_keywords'];
	$array['description']=$row['article_description'];
	$array['image']=$row['article_image'];
	$array['file']=$row['article_file'];
	$array['html']=$row['article_html'];
	$array['is_best']=$row['article_is_best'];
	$array['is_comment']=$row['article_is_comment'];
	$array['is_display']=$row['article_is_display'];
	$array['category_id']=$row['category_id'];

	$this->template->in('article',$array);
	$this->template->in('category_option',$content->category_option(0,$row['category_id']));
	$this->template->in('mode','update');
	$this->template->out('content.article.info.php');
}
//更新内容
if($this->do=='article_update'){
	$this->check_access('content_article');
	$this->set_hook('begin.article.update');
	$article_id=empty($_POST['article_id'])?0:intval($_POST['article_id']);
	$article_title=empty($_POST['article_title'])?'':trim(addslashes($_POST['article_title']));
	$article_content=empty($_POST['article_content'])?'':trim(addslashes($_POST['article_content']));
	$article_content_mode=empty($_POST['article_content_mode'])?0:1;
	$article_author=empty($_POST['article_author'])?'':trim(addslashes($_POST['article_author']));
	$article_keywords=empty($_POST['article_keywords'])?'':trim(addslashes($_POST['article_keywords']));
	$article_description=empty($_POST['article_description'])?'':trim(addslashes($_POST['article_description']));
	$article_file=empty($_POST['article_file'])?'':trim(addslashes($_POST['article_file']));
	$article_html=empty($_POST['article_html'])?'':trim(addslashes($_POST['article_html']));
	$article_html_old=empty($_POST['article_html_old'])?'':trim($_POST['article_html_old']);
	$article_image=empty($_POST['article_image'])?'':trim(addslashes($_POST['article_image']));
	$article_image_old=empty($_POST['article_image_old'])?'':trim(addslashes($_POST['article_image_old']));
	$article_image_delete=empty($_POST['article_image_delete'])?'':trim(addslashes($_POST['article_image_delete']));

	$article_is_best=empty($_POST['article_is_best'])?0:1;
	$article_is_comment=empty($_POST['article_is_comment'])?0:1;
	$article_is_display=empty($_POST['article_is_display'])?0:1;
	$category_id=empty($_POST['category_id'])?0:intval($_POST['category_id']);
	if($article_title==''){
		alert('标题不能为空');
	}
	if($article_content==''){
		alert('内容不能为空');
	}
	#本地方式上传
	$article_image_upload=upload($_FILES['article_image_upload'],HTML_DIR.'uploads/','jpg,png,gif,bmp',2);
	if(!empty($article_image_upload)){
		$article_image=PATH.HTML_DIR.'uploads/'.$article_image_upload;
	}
	#删除原始图像
	if(!empty($article_image_old)){
		if($article_image!=$article_image_old){
			@unlink(ROOT.'/'.$article_image_old);
		}
	}
	if(!empty($article_image_delete)){
		@unlink(ROOT.'/'.$article_image_delete);
		$article_image='';
	}
	#要是生成文件名被修改则删除旧文件
	if($article_html_old!=$article_html){
		$path='core/'.$content->category_parent_path($category_id);
		@unlink(ROOT.'/'.$path.'/'.$article_html_old);
	}
	$array=array();
	$array['article_title']=$article_title;
	$array['article_content']=$article_content;
	$array['article_content_mode']=$article_content_mode;
	$array['article_author']=$article_author;
	$array['article_keywords']=$article_keywords;
	$array['article_description']=$article_description;
	$array['article_file']=$article_file;
	$array['article_html']=$article_html;
	$array['article_image']=$article_image;
	$array['article_is_best']=$article_is_best;
	$array['article_is_comment']=$article_is_comment;
	$array['article_is_display']=$article_is_display;
	$array['category_id']=$category_id;
	$this->db->update(DB_PREFIX."content_article",$array,"article_id=$article_id");
	#把关键字加入标签库
	if(!empty($_POST['tag'])&&!empty($article_keywords)){
		$tag_explode=explode(",",$article_keywords);
		if(count($tag_explode)>0){
			foreach($tag_explode as $v){
				if(!empty($v)){
					$tag_link=PATH.HTML_DIR.$content->category_parent_path($category_id)."/".$article_html.".html";
					$this->db->insert(DB_PREFIX."tag",array('tag_name'=>$v,'tag_link'=>addslashes($tag_link)));
				}
			}
		}
	}
	$content->html_index();
	$content->html_article($article_id);
	$content->html_category_index($category_id);
	$content->html_category_list($category_id);
	$this->set_hook('end.article.update');
	redirect('?action=content&do=article');
}
if($this->do=='article_delete'){#删除内容
	$this->check_access('content_article');
	$this->set_hook('begin.article.delete');
	$article_id=empty($_POST['article_id'])?array():$_POST['article_id'];
	foreach($article_id as $value){
		if(!empty($value)){
			//判断内容是否有缩图，有就删除
			$row=$this->db->row("SELECT article_image,article_file FROM ".DB_PREFIX."content_article WHERE article_id='".$value."'");
			if(!empty($row['article_image'])){
				@unlink(ROOT."/".$row['article_image']);
			}
			//提取该内容附属的附件文件名并删除
			$file=explode(",",$row['article_file']);
			foreach($file as $v){
				@unlink(ROOT."/".$v);
			}
			#删除静态文件？还是不删除呢？
			#@unlink(ROOT."/content/".$this->category_parent_path($article['category_id'])."/".$article['article_html'].".html");
			$this->db->delete(DB_PREFIX."content_comment","article_id=".$value."");
			$this->db->delete(DB_PREFIX."content_article","article_id=".$value."");
		}
	}
	$this->html_index();
	$this->set_hook('end.article.delete');
	redirect('?action=content&do=article');
}
if($this->do=='get_description'){
	$this->check_access('content_article');
	$content=isset($_POST['content'])?trim($_POST['content']):'';
	echo truncate(strip_tags($content),100);
	exit;
}
if($this->do=='get_html'){
	$this->check_access('content_article');
	$content=isset($_POST['content'])?trim($_POST['content']):'';
	$content=str_replace(",","-",$content);
	echo pinyin($content);
	exit;
}
if($this->do=='check_html'){
	$this->check_access('content_article');
	$id=isset($_GET['id'])?intval($_GET['id']):0;
	$content=isset($_GET['content'])?trim($_GET['content']):'';
	$sql="SELECT * FROM ".DB_PREFIX."content_article WHERE article_html='$content'";
	if($id>0){
		$sql.=" AND article_id!=$id ";
	}
	$sql.=" LIMIT 0,1";
	$row=$this->db->row($sql);
	if($row){
		echo'YES';
	}
	exit;
}
if($this->do=='clear'){
	$this->check_access('content_article');
	$content->html_clear();
	redirect('?action=content&do=article');
	exit;
}
if($this->do=='html'){
	$this->check_access('content_article');
	$article_id=empty($_POST['article_id'])?array():$_POST['article_id'];
	$condition = "article_id in ( ";
	foreach($article_id as $value){
		if(!empty($value)){
			$content->html_article($value);
			$condition .= "'" . $value . "',";
		}
	}
	$condition = substr($condition, 0, strlen($condition)-1);
	$condition.=")";
	$this->db->update(DB_PREFIX . 'content_article', 'article_is_html = 1', $condition);
	// -- update html state
	
	
	$content->html_index();
	$this->set_hook('batch.html.article');
	redirect('?action=content&do=article');
}
if($this->do=='delete_file'){
	$this->check_access('content_article');
	$filename=empty($_GET['filename'])?'':trim($_GET['filename']);
	if(file_exists(ROOT.$filename)){
		@unlink(ROOT.$filename);
	}
	exit;
}
?>